package com.info.factory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Locale;

import com.info.BonusTypeEnum;
import com.info.ConditionalBonusInfo;
import com.info.FeatInfo;
import com.info.FeatTypeEnum;
import com.info.FocusFeatInfo;
import com.info.factory.db.FeatFactoryDbHelper;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class FeatFactory {

	private static FeatFactory INSTANCE;
	private FeatFactoryDbHelper helper;
	
	private String nameColumn[] = {TableColumn.NAME.name().toLowerCase(Locale.CANADA)};
	
	private enum TableColumn{
		ID,
		NAME,
		TYPE,
		DESCRIPTION,
		PREREQUISITE_ALL,
		PREREQUISITE_FEAT,
		BENEFIT,
		NORMAL,
		SPECIAL,
		SOURCE,
		FULL_TEXT,
		TEAMWORK,
		CRITICAL,
		GRIT,
		STYLE,
		PERFORM,
		RACIAL,
		COMPANION,
		RACE_NAME,
		NOTE,
		GOAL,
		COMPLETION_BENEFIT
	}
	
	public static FeatFactory getInstace(Context context){
		if (INSTANCE == null){
			INSTANCE = new FeatFactory(context);
		}
		return INSTANCE;
	}
	
	private FeatFactory(Context context){
		
	}
	
	public FeatInfo getFeat(String name){
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor c = database.query(FeatFactoryDbHelper.TABLE_NAME, nameColumn, TableColumn.NAME.name().toLowerCase(Locale.CANADA) + " = " + name,null, null, null, null);
		if (c.getCount() != 1){
			c.close();
			return null;
		}
		return buildFeat(c);
	}
	
	public FeatInfo buildFeat(Cursor c){
		String description = c.getString(TableColumn.DESCRIPTION.ordinal());
		FeatInfo info = null;
		if (description.indexOf("Choose") == 0){
			info = new FocusFeatInfo();
		}else{
			info = new FeatInfo();
		}
		info.setName(c.getString(TableColumn.NAME.ordinal()));
		info.setFeatType(buildFeatType(c.getString(TableColumn.TYPE.ordinal())));
		info.setDescription(description);
		info.setFeatPreReq(preReqFeat(c.getString(TableColumn.PREREQUISITE_FEAT.ordinal())));
		
		info.setBonus(buildFeatBonus(c.getString(TableColumn.BENEFIT.ordinal())));
		
		// Text
		info.setBenefitText(c.getString(TableColumn.BENEFIT.ordinal()));
		info.setNormalText(c.getString(TableColumn.NORMAL.ordinal()));
		info.setSpecialText(c.getString(TableColumn.SPECIAL.ordinal()));
		info.setSource(c.getString(TableColumn.SOURCE.ordinal()));
		
		// Flags
		info.setTeamwork(Boolean.parseBoolean(c.getString(TableColumn.TEAMWORK.ordinal())));
		info.setCritical(Boolean.parseBoolean(c.getString(TableColumn.CRITICAL.ordinal())));
		info.setGrit(Boolean.parseBoolean(c.getString(TableColumn.GRIT.ordinal())));
		info.setStyle(Boolean.parseBoolean(c.getString(TableColumn.STYLE.ordinal())));
		info.setPerform(Boolean.parseBoolean(c.getString(TableColumn.PERFORM.ordinal())));
		info.setRacial(Boolean.parseBoolean(c.getString(TableColumn.RACIAL.ordinal())));
		info.setCompanion(Boolean.parseBoolean(c.getString(TableColumn.COMPANION.ordinal())));
		info.setRaceName(c.getString(TableColumn.RACE_NAME.ordinal()));
		c.close();
		return null;
	}
	
	private Hashtable<BonusTypeEnum, HashSet<ConditionalBonusInfo>> buildFeatBonus(String benefit){
		return null;
	}
	
	private ArrayList<FeatInfo> preReqFeat(String preReq){
		ArrayList<FeatInfo> feat = new ArrayList<FeatInfo>();
		
		String feats[] = preReq.split(",");
		for (String s : feats){
			if (s.contains("(") || s.contains(")")){
				feat.add(FeatFactory.INSTANCE.getFeat(s.substring(0, s.indexOf("(") - 1).trim()));
			}else{
				feat.add(FeatFactory.INSTANCE.getFeat(s.trim()));
			}
		}
		
		return feat;
	}
	
	private HashSet<FeatTypeEnum> buildFeatType(String typeStr){
		HashSet<FeatTypeEnum> type = new HashSet<FeatTypeEnum>();
		String split[] = typeStr.split(",");
		for (String s : split){
			type.add(FeatTypeEnum.valueOf(s.trim().toUpperCase(Locale.CANADA)));
		}
		
		return type;
	}
}
